package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSave;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务，参数：{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        return list;
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("更新电子围栏状态业务，参数：id={},status={}", id, status);
        geofenceMapper.updateStatus(id, status, new Date());
    }

    @Override
    public void delete(Long id) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list =  vehicleMapper.selectVehicle(vehicleQuery);
        if (list.size() > 0){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else {
            geofenceMapper.delete(id);
        }


    }

    @Override
    public void save(GeofenceSave geofenceSave) {
        log.debug("保存电子围栏业务，参数：{}", geofenceSave);
        geofenceSave.setStatus("1");
        geofenceSave.setCreateTime(new Date());
        geofenceMapper.insert(geofenceSave);
    }
}
